package at.favre.lib.armadillo;

import org.jetbrains.annotations.Nullable;
import at.favre.lib.bytes.Bytes;
import ohos.app.Context;
import ohos.system.DeviceInfo;

/**
 * Factory for creating {@link EncryptionFingerprintFactory} in openharmony
 *
 * @author Patrick Favre-Bulle
 * @since 19.12.2017
 */

public final class EncryptionFingerprintFactory {

    private EncryptionFingerprintFactory() {
    }

    /**
     * Creates a new {@link EncryptionFingerprint} including the following data:
     * <p>
     * It is recommended to provide additional data unique to your domain (e.g. userId, api version, etc).
     * <p>
     * <em>Note:</em> If the fingerprint changes the data cannot be decrypted anymore.
     *
     * @param context        used to gather data from the openharmony framework
     * @param additionalData additional data provided by the caller
     * @return fingerprint
     */
    public static EncryptionFingerprint create(Context context, @Nullable String additionalData) {
        return new EncryptionFingerprint.Default(Bytes.from(
                Bytes.from(getApplicationPackage(context)).array(),
                Bytes.from(getBuildDetails()).array(),
                Constant.STATIC_RANDOM,
                additionalData != null ? Bytes.from(additionalData).array() : Bytes.empty().array()).array());
    }

    /**
     * Get some OS build data
     *
     * @return string including the info
     */
    private static String getBuildDetails() {
        return DeviceInfo.getName() + DeviceInfo.getModel() + DeviceInfo.getDeviceType();
    }

    /**
     * Gets the application package name e.g. "at.favre.app"
     *
     * @param context from openharmony
     * @return package
     */
    private static String getApplicationPackage(Context context) {
        return String.valueOf(context.getApplicationContext().getBundleName());
    }
}
